import hashlib
import os
from typing import List


def split_file(input_file: str, output_prefix: str, chunk_size: int) -> List[dict]:
    """
    将大文件切分为多个小文件

    参数:
        input_file: 输入文件路径
        output_prefix: 输出文件前缀
        chunk_size: 每个切分文件的大小(字节)
    """
    chunks: List[dict] = []
    with open(input_file, 'rb') as f:
        part_num = 0
        # 获取文件名
        file_name = os.path.basename(input_file)
        while True:
            chunk = f.read(chunk_size)
            if not chunk:
                break
            file = f"{file_name}-chunk-{part_num}"
            output_file = f"{output_prefix}/{file}"
            with open(output_file, 'wb') as out:
                out.write(chunk)
                print(f"已创建分片: {output_file}")
                chunk_sha1: str = hashlib.sha1(chunk).hexdigest()
                data = {
                    "chunk_sha1": chunk_sha1,
                    "chunk_path": output_file,
                    "sort": part_num
                }
                chunks.append(data)
                part_num += 1
    return chunks


if __name__ == '__main__':

    files: [dict] = [
        {
            "file": "zh_core_web_sm-3.8.0-py3-none-any.whl",
            "out": "chinese",
        },
        {
            "file": "en_core_web_sm-3.8.0-py3-none-any.whl",
            "out": "english",
        }
    ]

    # 获取当前目录
    current_directory: str = os.getcwd()

    for file in files:
        # 计算当前文件的 hash 值
        file_hash: str = hashlib.sha1(open(file["file"], 'rb').read()).hexdigest()
        print(f"正在计算文件: {file_hash}")
        # 按行写入文件
        out_dir: str = os.path.join(current_directory, file["out"])
        # 哈希文件
        sha1_file: str = os.path.join(out_dir, "sha1.txt")
        with open(sha1_file, 'w') as f:
            f.write(file_hash)

        original_file: str = os.path.join(current_directory, file["file"])
        # 如果文件不存在，创建文件
        os.makedirs(out_dir, exist_ok=True)
        # 文件切分列表
        chunks: List[dict] = split_file(original_file, file["out"], 1024 * 1024 * 5)
        # 将数据列表转化为json字符串，并写入文件
        chunk_file: str = os.path.join(out_dir, "chunk.json")
        with open(chunk_file, 'w') as f:
            json: str = str(chunks)
            json = json.replace("'", "\"")
            f.write(json)
            print(json)
